package pkg

import (
	"errors"
	"fmt"
	"github.com/golang-jwt/jwt/v4"
	"time"
)

const AppKey = "tgsiehrou243hg780qpnv610nbqv-u89y7n0b8e7r7q3jiv4"

func GetToken(UserId int64, vip int64) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
		"user": UserId,
		"vip":  vip,
		"exp":  time.Now().Add(time.Hour * time.Duration(12)).Unix(),
		"iat":  time.Now().Unix(),
	})
	tokenString, err := token.SignedString([]byte(AppKey))
	return tokenString, err
}
func TokenData(tokens string) (jwt.MapClaims, error) {
	token, err := jwt.Parse(tokens, func(token *jwt.Token) (interface{}, error) {
		return []byte(AppKey), nil
	})

	if token.Valid {
		fmt.Println("You look nice today")
	} else if errors.Is(err, jwt.ErrTokenMalformed) {
		return nil, fmt.Errorf("That's not even a token")
	} else if errors.Is(err, jwt.ErrTokenUnverifiable) {
		return nil, fmt.Errorf("We could not verify this token")
	} else if errors.Is(err, jwt.ErrTokenSignatureInvalid) {
		return nil, fmt.Errorf("This token has an invalid signature")
	} else if errors.Is(err, jwt.ErrTokenExpired) || errors.Is(err, jwt.ErrTokenNotValidYet) {
		// Token is either expired or not active yet
		return nil, fmt.Errorf("Timing is everything")
	} else {
		return nil, fmt.Errorf("Couldn't handle this token:", err)
	}
	if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
		return claims, err
	} else {
		return nil, err
	}
}
